package com.cxh.service.impl;

import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.CarDiscountDao;
import com.cxh.dao.CarDiscountDetailDao;
import com.cxh.entity.CarDiscount;
import com.cxh.entity.CarDiscountDetail;
import com.cxh.service.CarDiscountService;
import com.cxh.util.CommonUtility;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;
import java.util.Map;

/**
 * 类说明：
 * @author  ChenXH
 * @date  2018-12-07 15:57:34
 */
@Service("carDiscountService")
@DataSource("dataSource2")
public class CarDiscountServiceImpl extends Data2ServiceImpl<CarDiscount> implements CarDiscountService {
    @Autowired
    private CarDiscountDao dao;
    @Autowired
    private CarDiscountDetailDao carDiscountDetailDao;
    

    @Override
    public List<CarDiscount> loadPageList(Map<String, Object> map) {
       return dao.loadPageList(map);
    }
    
    /**
     * @类功能说明：根据用户信息查询4s店的经销商,根据经销商查询优惠活动
     * @作者：Chenxh
     * @创建时间：2018年12月10日下午2:33:52
     */
    @Override
    public CarDiscount getCarDiscountByUserId(String userid) {
    	return dao.getCarDiscountByUserId(userid);
    }
    
    @Override
    public CarDiscount getcarDiscountByRobotCode(String robotCode) {
    	//根据机器人的code查询是哪个用户的
    	return dao.getcarDiscountByRobotCode(robotCode);
    }
    
    @Override
    public void update(CarDiscount carDiscount,CarDiscountDetail carDiscountDetail) {
         //更新主页的页面
         dao.updateByPrimaryKey(carDiscount);
         carDiscountDetail.setDiscountid(carDiscount.getId().toString());
         Example example = new Example(CarDiscountDetail.class);
         Criteria createCriteria = example.createCriteria();
         createCriteria.andEqualTo("discountid",carDiscount.getId().toString());
         List<CarDiscountDetail> carDiscountDetailList = carDiscountDetailDao.selectByExample(example);
         carDiscountDetail.setId(carDiscountDetailList.get(0).getId());
         
         //更新主页的小页面
         carDiscountDetailDao.updateByPrimaryKey(carDiscountDetail);
    }
    
    @Override
    public void addData(CarDiscount carDiscountSave,CarDiscountDetail carDiscountDetail) {
    	dao.insert(carDiscountSave);
    	carDiscountSave.getId();
    	carDiscountDetail.setDiscountid(carDiscountSave.getId().toString());
    	carDiscountDetailDao.insert(carDiscountDetail);
    }
    
    /**
     * @类功能说明：截取最后/以后的字符串
     * @作者：Chenxh
     * @创建时间：2018年12月11日上午10:42:01
     */
    public String getSubString(String str){
         int lastIndexOf = str.lastIndexOf("/");
         String subFirstimg = str.substring(lastIndexOf+1,str.length());
         return subFirstimg;
    }

}
